package com.bloveoacom.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.bloveoacom.base.AppContext;
import com.bloveoacom.http.MessageModel;
import com.bloveoacom.http.RxBus;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.ActivityDetailBean;
import com.bloveoacom.weichi.bean.BuyBean;
import com.bloveoacom.weight.MinusAndPlusBtn;
import com.bloveoacom.weight.RemindDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.widget.longimage.ImageSource;
import com.luck.picture.lib.widget.longimage.SubsamplingScaleImageView;


import java.io.File;
import java.util.List;


/**
 * Created by yujunlong on 2017/5/15.
 */

public class DialogUtil {

    public static Dialog getDialg(Context context, View view) {
        return getDialg(context,view,true,false);
    }

    public static Dialog getDialg(Context context, View view, boolean isMatchWidth, boolean isMathcHeight) {
        Dialog dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            if(isMatchWidth){
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            if(isMathcHeight){
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
            }

            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        return dialog;
    }

    public static Dialog getDialg2(Context context, View rsd) {
        Dialog dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.setContentView(rsd);
        return dialog;
    }


    public static BottomSheetDialog getPicBottomDialog(Context context, final PhotoDoWhat photoDoWhat, File path) {
        final BottomSheetDialog dialog = new BottomSheetDialog(context, R.style.MyDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_sharewebviewpic, null);
        SubsamplingScaleImageView imageView = ViewFindUtils.find(view, R.id.imageView);
        imageView.setImage(ImageSource.uri(Uri.fromFile(path)));
        ViewFindUtils.find(view, R.id.iv_wxfriend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.doone();
                }
            }
        });
        ViewFindUtils.find(view, R.id.iv_pyq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.dotwo();
                }
            }
        });
        ViewFindUtils.find(view, R.id.tv_save_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.dothree();
                }
            }
        });
        ViewFindUtils.find(view, R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.dissDialog(dialog);
            }
        });
        dialog.setContentView(view);
        return dialog;
    }

    public static Dialog getPicBottomDialog2(Context context, final PhotoDoWhat photoDoWhat, File path) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_sharewebviewpic, null);
        final Dialog dialog = getDialg(context, view,true,true);
        SubsamplingScaleImageView imageView = ViewFindUtils.find(view, R.id.imageView);
        imageView.setImage(ImageSource.uri(Uri.fromFile(path)));
        ViewFindUtils.find(view, R.id.iv_wxfriend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.doone();
                }
            }
        });
        ViewFindUtils.find(view, R.id.iv_pyq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.dotwo();
                }
            }
        });
        ViewFindUtils.find(view, R.id.tv_save_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(photoDoWhat!=null){
                    photoDoWhat.dothree();
                }
            }
        });
        ViewFindUtils.find(view, R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.dissDialog(dialog);
            }
        });
        dialog.setContentView(view);
        return dialog;
    }



    public static void dissDialog(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }





    public static void showDialog(Dialog dialog) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public static void showExitDialog() {
        final Activity activity = AppManager.getAppManager().getCurrentActivity();
        RemindDialog dialog = new RemindDialog(activity, null, "确定要退出登录", "取消", "确定", R.style.MyDialogStyle);
        dialog.setItemClickListener(new RemindDialog.ItemClickListener() {
            @Override
            public void left() {

            }

            @Override
            public void right() {
                AppContext.exit(activity);
                RxBus.getInstance().send(new MessageModel(MessageModel.TU_TYPE_EXIT));
                activity.finish();
            }
        });
        DialogUtil.showDialog(dialog);
    }

    public static Dialog getUpdateDialog(final Context c, String version_info) {
        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("更新提示");
        builder.setMessage("有新版本了，请及时更新");
        builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
//                Intent intent = new Intent(c, DownLoadService.class);
//                intent.putExtra(Constant.DATA,APK_DOWN_URL);
//                intent.putExtra(Constant.DATA2,"jdyx.apk");
//                c.startService(intent);
            }
        });
        builder.setNegativeButton("以后更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        Dialog noticeDialog = builder.create();
        noticeDialog.setCanceledOnTouchOutside(false);
        return noticeDialog;
    }
    public interface BuyDialogListener {
        void buy(BuyBean buyBean);
    }

    public static BottomSheetDialog getBottomBuyDialog(Context context, final BuyDialogListener buyDialogListener, ActivityDetailBean activityDetailBean) {
        BuyBean buyBean = new BuyBean();
        final BottomSheetDialog dialog = new BottomSheetDialog(context, R.style.MyDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_buy, null);
        RecyclerView rvPrice = (RecyclerView) ViewFindUtils.find(view, R.id.rv_price);
        ImageView ivPhoto=(ImageView)ViewFindUtils.find(view,R.id.iv_photo);
        TextView tvPrice=(TextView)ViewFindUtils.find(view,R.id.tv_price);
        MinusAndPlusBtn minusAndPlusBtn=(MinusAndPlusBtn)ViewFindUtils.find(view,R.id.btn_add_or_plus);

        GlideUtil.loadRoundmage(ivPhoto, StringUtils.getFullUrl(activityDetailBean.getImg(), 0));

        List<ActivityDetailBean.ActivityFareListBean> priceList=activityDetailBean.getActivityFareList();
        BaseQuickAdapter priceAdapter = new BaseQuickAdapter<ActivityDetailBean.ActivityFareListBean, BaseViewHolder>(R.layout.rv_item_price, priceList) {
            @Override
            protected void convert(BaseViewHolder helper, ActivityDetailBean.ActivityFareListBean activityFareListBean) {
                TextView tvPrice = helper.getView(R.id.tv_price);
                tvPrice.setText(String.format("¥%s", activityFareListBean.preferentialFaceString));
                if (activityFareListBean.isChecked()) {
                    tvPrice.setTextColor(ContextCompat.getColor(context, R.color.white));
                    tvPrice.setBackgroundResource(R.drawable.shape_price_checked);
                } else {
                    tvPrice.setTextColor(ContextCompat.getColor(context, R.color.black));
                    tvPrice.setBackgroundResource(R.drawable.shape_price);
                }
            }
        };
        rvPrice.setLayoutManager(new GridLayoutManager(context,2));
        rvPrice.setAdapter(priceAdapter);
        priceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < priceList.size(); i++) {
                    if (i == position) {
                        priceList.get(i).setChecked(true);
                        buyBean.num = String.valueOf(minusAndPlusBtn.getNumber());
                        buyBean.price = priceList.get(i).preferentialFaceString;
                        setText(tvPrice,priceList.get(i).preferentialFaceString,priceList.get(i).getFace(),minusAndPlusBtn.getNumber());
                    } else {
                        priceList.get(i).setChecked(false);
                    }
                }
                priceAdapter.notifyDataSetChanged();
            }
        });
        minusAndPlusBtn.setMinusAndPlusListener(new MinusAndPlusBtn.MinusAndPlusListener() {
            @Override
            public void minusAndPlusClickListener(int number) {
                for(int i=0;i<priceList.size();i++){
                    if(priceList.get(i).isChecked()){
                        setText(tvPrice,priceList.get(i).preferentialFaceString,priceList.get(i).getFace(),number);
                        buyBean.num = String.valueOf(minusAndPlusBtn.getNumber());
                        buyBean.price = priceList.get(i).preferentialFaceString;
                    }
                }
            }
        });
        ViewFindUtils.find(view, R.id.tv_buy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buyDialogListener != null) {
                    buyDialogListener.buy(buyBean);
                }
                dialog.dismiss();
            }
        });
        ViewFindUtils.find(view, R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        if(priceList!=null&&priceList.size()>0){
            priceList.get(0).setChecked(true);
            buyBean.num = String.valueOf(minusAndPlusBtn.getNumber());
            buyBean.price = priceList.get(0).preferentialFaceString;
            setText(tvPrice,priceList.get(0).preferentialFaceString,priceList.get(0).getFace(),minusAndPlusBtn.getNumber());
        }
        dialog.setContentView(view);
        return dialog;


    }
    public static void setText(TextView tvPrice,String preferentialFaceString,String face,int number){
        String totalPrice=String.valueOf(NumberUtil.mul(preferentialFaceString,String.valueOf(number)));
        String totalPrice2 =String.valueOf(NumberUtil.mul(face,String.valueOf(number)));
        tvPrice.setText(new SpanUtils().appendLine("¥"+totalPrice).appendLine("").append("原价"+totalPrice2).setForegroundColor(ContextCompat.getColor(tvPrice.getContext(),R.color.color_666666)).setStrikethrough().create());
    }


    public static void checkPer(final Context c, final Dialog dialog) {

    }

    public interface TimeDoWhat {
        void doOne(String data, long time);
    }

    //弹框之前记得权限问题 Manifest.permission.WRITE_EXTERNAL_STORAGE
    public interface OpDoWhat {
        void doOne(int pos1, int pos2, String data1, String data2);
    }

    public interface PhotoDoWhat {
        void doone();

        void dotwo();
        void dothree();
    }
}
